import axios from 'axios'
import baseURL from './baseURL'
import routerIndex from '../router'
import Qs from 'qs'

var instace = axios.create({
  method: 'post',
  baseURL,
  headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
  transformRequest: [data => Qs.stringify(data)],
  paramsSerializer: params => Qs.stringify(params),
  params: {},
  data: {},
  withCredentials: true,
  timeout: 3000
})
// 请求拦截
instace.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})
// 响应拦截
instace.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  if (response.data && response.data.code) {
    if (response.data.code === 999) {
      routerIndex.push('/')
    }
  }
  return response
}, function (error) {
  return Promise.reject(error)
})
export default instace
